package org.uninova.mobis.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.uninova.mobis.utils.DBUtilsImpl;

import cc.curiouscat.types.User;

public class UserDBConnector {
	
	public UserDBConnector() {}
	
	public boolean userExists(String ccToken) {
		DBUtilsImpl dbUtils = new DBUtilsImpl() ;
		
		Connection con = dbUtils.startConnection() ;
		
		String sqlSelectUser = "SELECT * FROM mobis_db.user WHERE cc_token=" + ccToken ;
		
		ResultSet set = dbUtils.queryDB(con, sqlSelectUser) ;
		try {
			if (set.wasNull()) {
				return false ;
			}
			else return true ;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false ;
	}
	
	public void createUser(User user) {
		DBUtilsImpl dbUtils = new DBUtilsImpl() ;
		
		Connection con = dbUtils.startConnection() ;
		
		String sqlCreateUser = "INSERT INTO mobis_db.user (userid, cc_token, first_name, last_name) VALUES (" + user.getId() + "," + user.getToken() + "," + user.getName() + "," + user.getSurname() + ") ;" ;
		
		dbUtils.modifyDB(con, sqlCreateUser) ;
	}
}
